// 组件管理器 - 统一管理所有组件的独立配置
import { BusinessAnalysisConfig } from './BusinessAnalysis/config.js'
import { CustomerInsightConfig } from './CustomerInsight/config.js'
import { WarningMonitorConfig } from './WarningMonitor/config.js'

// 组件配置映射
export const ComponentConfigs = {
    BusinessAnalysis: BusinessAnalysisConfig,
    CustomerInsight: CustomerInsightConfig,
    WarningMonitor: WarningMonitorConfig
}

// 组件样式映射
export const ComponentStyles = {
    BusinessAnalysis: './BusinessAnalysis/style.css',
    CustomerInsight: './CustomerInsight/style.css',
    WarningMonitor: './WarningMonitor/style.css'
}

// 组件管理器类
export class ComponentManager {
    constructor() {
        this.configs = ComponentConfigs
        this.styles = ComponentStyles
        this.charts = new Map()
    }

    // 获取组件配置
    getConfig(componentName) {
        return this.configs[componentName] || {}
    }

    // 获取组件样式路径
    getStylePath(componentName) {
        return this.styles[componentName] || ''
    }

    // 注册图表实例
    registerChart(componentName, chartName, chartInstance) {
        const key = `${componentName}_${chartName}`
        this.charts.set(key, chartInstance)
    }

    // 获取图表实例
    getChart(componentName, chartName) {
        const key = `${componentName}_${chartName}`
        return this.charts.get(key)
    }

    // 销毁所有图表
    destroyAllCharts() {
        this.charts.forEach(chart => {
            if (chart && typeof chart.dispose === 'function') {
                chart.dispose()
            }
        })
        this.charts.clear()
    }

    // 销毁指定组件的图表
    destroyComponentCharts(componentName) {
        const keysToDelete = []
        this.charts.forEach((chart, key) => {
            if (key.startsWith(componentName)) {
                if (chart && typeof chart.dispose === 'function') {
                    chart.dispose()
                }
                keysToDelete.push(key)
            }
        })
        keysToDelete.forEach(key => this.charts.delete(key))
    }

    // 刷新指定组件的图表
    refreshComponentCharts(componentName) {
        this.charts.forEach((chart, key) => {
            if (key.startsWith(componentName)) {
                if (chart && typeof chart.resize === 'function') {
                    chart.resize()
                }
            }
        })
    }

    // 获取所有组件名称
    getComponentNames() {
        return Object.keys(this.configs)
    }

    // 获取组件配置摘要
    getComponentSummary() {
        const summary = {}
        Object.keys(this.configs).forEach(name => {
            const config = this.configs[name]
            summary[name] = {
                charts: Object.keys(config).filter(key => key !== 'container'),
                hasConfig: true,
                hasStyle: !!this.styles[name]
            }
        })
        return summary
    }

    // 验证组件配置
    validateConfig(componentName) {
        const config = this.configs[componentName]
        if (!config) {
            return { valid: false, error: `组件 ${componentName} 不存在` }
        }

        const requiredFields = ['container']
        const missingFields = requiredFields.filter(field => !config[field])

        if (missingFields.length > 0) {
            return {
                valid: false,
                error: `组件 ${componentName} 缺少必需字段: ${missingFields.join(', ')}`
            }
        }

        return { valid: true }
    }

    // 更新组件配置
    updateConfig(componentName, newConfig) {
        if (this.configs[componentName]) {
            this.configs[componentName] = { ...this.configs[componentName], ...newConfig }
            return true
        }
        return false
    }

    // 重置组件配置
    resetConfig(componentName) {
        if (this.configs[componentName]) {
            // 重新导入原始配置
            switch (componentName) {
                case 'BusinessAnalysis':
                    this.configs[componentName] = BusinessAnalysisConfig
                    break
                case 'CustomerInsight':
                    this.configs[componentName] = CustomerInsightConfig
                    break
                case 'WarningMonitor':
                    this.configs[componentName] = WarningMonitorConfig
                    break
            }
            return true
        }
        return false
    }

    // 导出组件配置
    exportConfig(componentName) {
        return this.configs[componentName] ? JSON.stringify(this.configs[componentName], null, 2) : null
    }

    // 导入组件配置
    importConfig(componentName, configJson) {
        try {
            const config = JSON.parse(configJson)
            this.configs[componentName] = config
            return true
        } catch (error) {
            console.error('配置导入失败:', error)
            return false
        }
    }
}

// 创建全局组件管理器实例
export const componentManager = new ComponentManager()

// 默认导出
export default componentManager 